package com.zide.member.service.community.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.zide.auth.utils.ZiDeContext;
import com.zide.common.core.domain.PageResult;
import com.zide.common.utils.DateUtils;
import com.zide.member.entity.knowledge.CommunityEssay;
import com.zide.member.mapper.community.CommunityEssayMapper;
import com.zide.member.service.community.CommunityEssayService;
import com.zide.member.vo.result.community.CommEssayResultVO;
import com.zide.member.vo.save.community.CommEssaySaveVO;
import com.zide.member.vo.search.community.CommEssaySearchVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Arrays;

/**
 * @author 石添添
 * @description
 * @date 2024-09-09 21:56
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class CommunityEssayServiceImpl implements CommunityEssayService {
    private final CommunityEssayMapper communityEssayMapper;
    @Override
    public PageResult<CommEssayResultVO> pageList(CommEssaySearchVO searchVO) {

        return communityEssayMapper.selectPage(searchVO);
    }

    /**
     * 发布社区文章
     */
    @Override
    public int pushEssay(CommEssaySaveVO saveVO) {
        Long memberUserId = ZiDeContext.getMemberUserId();
        CommunityEssay insertBean = BeanUtil.copyProperties(saveVO, CommunityEssay.class);
        insertBean.setMemberId(memberUserId);
        return communityEssayMapper.insert(insertBean);
    }

    /**
     * 修改社区文章
     */
    @Override
    public int modifyEssay(CommEssaySaveVO saveVO) {
        CommunityEssay updateBean = BeanUtil.copyProperties(saveVO, CommunityEssay.class);
        return communityEssayMapper.updateById(updateBean);
    }

    /**
     * 删除社区文章
     */
    @Override
    public int removeEssay(Long[] ids) {
        return communityEssayMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 查看文章详情
     * @param id
     * @return
     */
    @Override
    public CommEssayResultVO searchDetail(Long id) {

        CommunityEssay communityEssay = communityEssayMapper.selectById(id);
        if(ObjectUtil.isNotNull(communityEssay)) {
            CommEssayResultVO result = BeanUtil.copyProperties(communityEssay, CommEssayResultVO.class);
            result.setPushTime(communityEssay.getCreateTime());
            String currentInterval = DateUtils.getTimeDiffToMil(communityEssay.getUpdateTime());
            log.info("相隔时间:{}",currentInterval);
            result.setIntervalTime(currentInterval);
            return result;
        }
        return null;
    }
}
